package com.ymatou.iapi.livebuyer.testcase;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.livebuyer.parameter.AskProductVideoBean;
import com.ymatou.iapi.livebuyer.service.AskProductVideoCall;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CounterCallService;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.model.livetv.ProductVideoExplain;
import com.ymttest.database.sqlwapper.LiveRoomWapper;
import com.ymttest.utils.EnvSetup;

/**
 * 求讲解 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_AskProductVideo {
	private static AskProductVideoBean askproductvideoBean;
	private static AskProductVideoCall askproductvideoCall;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	private static LiveRoomWapper LiveRoomWapper;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("求讲解");
	}

	@Before
	public void caseUp() {
		askproductvideoBean = new AskProductVideoBean();
		askproductvideoCall = new AskProductVideoCall();
		LiveRoomWapper = new LiveRoomWapper();

	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(askproductvideoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_AskProductVideo_001() {
		Logger.start(true, "求讲解成功");
		try {
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String pid = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(sellerId));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), createlive.getUserId());
			askproductvideoBean.setLiveId(createlive.getId());
			askproductvideoBean.setProductId(pid);
			// 普通用户id 暂时无用
			askproductvideoBean.setUserId(1);
			askproductvideoCall.setData(askproductvideoBean);
			askproductvideoCall.callService();
			Logger.verifyEquals("200", askproductvideoCall.getString("code"), "验证code");
			ProductVideoExplain pve = LiveRoomWapper.selectProductvideoExplain(pid);
			Logger.verifyEquals(1, pve.getExplainNum(), "验证ExplainNum");
			Logger.verifyEquals(sellerId, pve.getUserId(), "验证userid");
			// 0 待录制
			Logger.verifyEquals(0, pve.getExplainStatus(), "验证explainStatus");
			Logger.verifyEquals(createlive.getId(), pve.getLiveId(), "验证LiveId");
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_AskProductVideo_002() {
		Logger.start(true, "不输入userid失败");
		try {
			askproductvideoBean.setLiveId(1);
			askproductvideoBean.setProductId("1");
			// 普通用户id 暂时无用
			// askproductvideoBean.setUserId(1);
			askproductvideoCall.setData(askproductvideoBean);
			askproductvideoCall.callService();
			Logger.verifyEquals("100", askproductvideoCall.getString("code"), "验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_AskProductVideo_003() {
		Logger.start(true, "不输入liveid失败");
		try {
			// askproductvideoBean.setLiveId(1);
			askproductvideoBean.setProductId("1");
			// 普通用户id 暂时无用
			askproductvideoBean.setUserId(1);
			askproductvideoCall.setData(askproductvideoBean);
			askproductvideoCall.callService();
			Logger.verifyEquals("100", askproductvideoCall.getString("code"), "验证code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_AskProductVideo_004() {
		Logger.start(true, "不输入productid失败");
		try {
			askproductvideoBean.setLiveId(1);
			// askproductvideoBean.setProductId("1");
			// 普通用户id 暂时无用
			askproductvideoBean.setUserId(1);
			askproductvideoCall.setData(askproductvideoBean);
			askproductvideoCall.callService();
			Logger.verifyEquals("100", askproductvideoCall.getString("code"), "验证code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	@Ignore
	// uid是用户id 无此场景
	public void Tc_AskProductVideo_005() {
		Logger.start(true, "userid和liveid不匹配失败");
		try {
			org.junit.Assert.fail();
			askproductvideoCall.setData(askproductvideoBean);
			askproductvideoCall.callService();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_AskProductVideo_006() {
		Logger.start(true, "直播已结束失败");
		try {
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String pid = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(sellerId));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), createlive.getUserId());
			LiveRoomCallService.AutoCloseLiveRoomByUserId(sellerId);
			askproductvideoBean.setLiveId(createlive.getId());
			askproductvideoBean.setProductId(pid);
			// 普通用户id 暂时无用
			askproductvideoBean.setUserId(1);
			askproductvideoCall.setData(askproductvideoBean);
			askproductvideoCall.callService();
			Logger.verifyEquals("200", askproductvideoCall.getString("code"), "验证code");
			ProductVideoExplain pve = LiveRoomWapper.selectProductvideoExplain(pid);
			Logger.verifyIsNull(pve, "已结束不进数据");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_AskProductVideo_007() {
		Logger.start(true, "直播未开始");
		try {
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String pid = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(sellerId));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
			// LiveRoomCallService.SellerLiveOnline(createlive.getId(),
			// createlive.getUserId());
			// LiveRoomCallService.SysCloseLive(createlive.getId());
			askproductvideoBean.setLiveId(createlive.getId());
			askproductvideoBean.setProductId(pid);
			// 普通用户id 暂时无用
			askproductvideoBean.setUserId(1);
			askproductvideoCall.setData(askproductvideoBean);
			askproductvideoCall.callService();
			Logger.verifyEquals("200", askproductvideoCall.getString("code"), "验证code");
			ProductVideoExplain pve = LiveRoomWapper.selectProductvideoExplain(pid);
			Logger.verifyIsNull(pve, "未开始不进数据");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_AskProductVideo_008() {
		Logger.start(true, "数据已存在,数量加一");
		try {
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String pid = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(sellerId));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), createlive.getUserId());
			askproductvideoBean.setLiveId(createlive.getId());
			askproductvideoBean.setProductId(pid);
			// 普通用户id 暂时无用
			askproductvideoBean.setUserId(1);
			askproductvideoCall.setData(askproductvideoBean);
			askproductvideoCall.callService();
			Logger.verifyEquals("200", askproductvideoCall.getString("code"), "验证code");
			ProductVideoExplain pve = LiveRoomWapper.selectProductvideoExplain(pid);
			Logger.verifyEquals(1, pve.getExplainNum(), "验证ExplainNum");
			Logger.verifyEquals(sellerId, pve.getUserId(), "验证userid");
			// 0 待录制
			Logger.verifyEquals(0, pve.getExplainStatus(), "验证explainStatus");
			Logger.verifyEquals(createlive.getId(), pve.getLiveId(), "验证LiveId");

			askproductvideoBean.setUserId(2);
			askproductvideoCall.setData(askproductvideoBean);
			askproductvideoCall.callService();
			Logger.verifyEquals("200", askproductvideoCall.getString("code"), "验证code");
			pve = LiveRoomWapper.selectProductvideoExplain(pid);
			Logger.verifyEquals(2, pve.getExplainNum(), "验证ExplainNum");
			Logger.verifyEquals(sellerId, pve.getUserId(), "验证userid");
			// 0 待录制
			Logger.verifyEquals(0, pve.getExplainStatus(), "验证explainStatus");
			Logger.verifyEquals(createlive.getId(), pve.getLiveId(), "验证LiveId");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_AskProductVideo_009() {
		Logger.start(true, "同一用户多次求讲解同一商品视频");
		try {
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String pid = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(sellerId));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), createlive.getUserId());
			askproductvideoBean.setLiveId(createlive.getId());
			askproductvideoBean.setProductId(pid);
			// 普通用户id 暂时无用
			askproductvideoBean.setUserId(1);
			askproductvideoCall.setData(askproductvideoBean);
			askproductvideoCall.callService();
			askproductvideoCall.callService();
			askproductvideoCall.callService();
			Logger.verifyEquals("200", askproductvideoCall.getString("code"), "验证code");
			ProductVideoExplain pve = LiveRoomWapper.selectProductvideoExplain(pid);
			Logger.verifyEquals(1, pve.getExplainNum(), "验证ExplainNum");
			Logger.verifyEquals(sellerId, pve.getUserId(), "验证userid");
			// 0 待录制
			Logger.verifyEquals(0, pve.getExplainStatus(), "验证explainStatus");
			Logger.verifyEquals(createlive.getId(), pve.getLiveId(), "验证LiveId");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_AskProductVideo_010() {
		Logger.start(true, "同一用户多次求讲解多个商品视频");
		try {
			List<String> pids = new ArrayList<>();
			for (int i = 0; i < 3; i++) {
				AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
				String pid = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
				Logger.comment("创建商品：" + pid);
				pids.add(pid);
			}
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(sellerId));
			pids.forEach(pid -> {
				try {
					LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
				} catch (IOException e) {
					e.printStackTrace();
				}
			});

			LiveRoomCallService.SellerLiveOnline(createlive.getId(), createlive.getUserId());
			pids.forEach(pid -> {
				askproductvideoBean.setLiveId(createlive.getId());
				askproductvideoBean.setProductId(pid);
				// 普通用户id 暂时无用
				askproductvideoBean.setUserId(1);
				askproductvideoCall.setData(askproductvideoBean);
				try {
					askproductvideoCall.callService();
				} catch (IOException e) {
					e.printStackTrace();
				}
				Logger.verifyEquals("200", askproductvideoCall.getString("code"), "验证code");
			});
			pids.forEach(pid -> {
				ProductVideoExplain pve = LiveRoomWapper.selectProductvideoExplain(pid);
				Logger.verifyEquals(1, pve.getExplainNum(), "验证ExplainNum");
				Logger.verifyEquals(sellerId, pve.getUserId(), "验证userid");
				// 0 待录制
				Logger.verifyEquals(0, pve.getExplainStatus(), "验证explainStatus");
				Logger.verifyEquals(createlive.getId(), pve.getLiveId(), "验证LiveId");
			});

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_AskProductVideo_011() {
		Logger.start(true, "多个用户求讲解同一商品视频");
		try {
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String pid = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(sellerId));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), createlive.getUserId());
			askproductvideoBean.setLiveId(createlive.getId());
			askproductvideoBean.setProductId(pid);
			for (int i = 1; i < 10; i++) {
				// 普通用户id 暂时无用
				askproductvideoBean.setUserId(i);
				askproductvideoCall.setData(askproductvideoBean);
				askproductvideoCall.callService();
				Logger.verifyEquals("200", askproductvideoCall.getString("code"), "验证code");
			}
			
			ProductVideoExplain pve = LiveRoomWapper.selectProductvideoExplain(pid);
			Logger.verifyEquals(9, pve.getExplainNum(), "验证ExplainNum");
			Logger.verifyEquals(sellerId, pve.getUserId(), "验证userid");
			// 0 待录制
			Logger.verifyEquals(0, pve.getExplainStatus(), "验证explainStatus");
			Logger.verifyEquals(createlive.getId(), pve.getLiveId(), "验证LiveId");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_AskProductVideo_012() {
		Logger.start(true, "商品在A直播中被求讲解A活动结束后，在B直播中同一商品被求讲解");
		try {
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String pid = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(sellerId));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), createlive.getUserId());
			askproductvideoBean.setLiveId(createlive.getId());
			askproductvideoBean.setProductId(pid);
			// 普通用户id 暂时无用
			askproductvideoBean.setUserId(1);
			askproductvideoCall.setData(askproductvideoBean);
			askproductvideoCall.callService();
			Logger.verifyEquals("200", askproductvideoCall.getString("code"), "验证code");
			ProductVideoExplain pve = LiveRoomWapper.selectProductvideoExplain(pid);
			Logger.verifyEquals(1, pve.getExplainNum(), "验证ExplainNum");
			Logger.verifyEquals(sellerId, pve.getUserId(), "验证userid");
			// 0 待录制
			Logger.verifyEquals(0, pve.getExplainStatus(), "验证explainStatus");
			Logger.verifyEquals(createlive.getId(), pve.getLiveId(), "验证LiveId");
			// 第二次
			createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(sellerId));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), createlive.getUserId());
			askproductvideoBean.setLiveId(createlive.getId());
			askproductvideoBean.setProductId(pid);
			// 普通用户id 暂时无用
			askproductvideoBean.setUserId(1);
			askproductvideoCall.setData(askproductvideoBean);
			askproductvideoCall.callService();
			Logger.verifyEquals("200", askproductvideoCall.getString("code"), "验证code");
			pve = LiveRoomWapper.selectProductvideoExplain(pid);
			Logger.verifyEquals(1, pve.getExplainNum(), "验证ExplainNum");
			Logger.verifyEquals(sellerId, pve.getUserId(), "验证userid");
			// 0 待录制
			Logger.verifyEquals(0, pve.getExplainStatus(), "验证explainStatus");
			Logger.verifyEquals(createlive.getId(), pve.getLiveId(), "验证LiveId");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_AskProductVideo_013() {
		Logger.start(true, "求讲解之后，入库askproductvideo表");
		try {
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String pid = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(sellerId));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), createlive.getUserId());
			askproductvideoBean.setLiveId(createlive.getId());
			askproductvideoBean.setProductId(pid);
			// 普通用户id 暂时无用
			
			askproductvideoBean.setUserId(1);
			askproductvideoCall.setData(askproductvideoBean);
			askproductvideoCall.callService();
			Logger.verifyEquals("200", askproductvideoCall.getString("code"), "验证code");
			ProductVideoExplain pve = LiveRoomWapper.selectProductvideoExplain(pid);
			Logger.verifyEquals(1, pve.getExplainNum(), "验证ExplainNum");
			Logger.verifyEquals(sellerId, pve.getUserId(), "验证userid");
			// 0 待录制
			Logger.verifyEquals(0, pve.getExplainStatus(), "验证explainStatus");
			Logger.verifyEquals(createlive.getId(), pve.getLiveId(), "验证LiveId");
			// 第二次
			createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(sellerId));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), createlive.getUserId());
			Thread.sleep(5000);
			
			System.out.println(pid+"---"+askproductvideoBean.getLiveId()+"---"+createlive.getId());
			askproductvideoBean.setLiveId(createlive.getId());
			askproductvideoBean.setProductId(pid);
			// 普通用户id 暂时无用
			askproductvideoBean.setUserId(1);
			askproductvideoCall.setData(askproductvideoBean);
			askproductvideoCall.callService();
			Logger.verifyEquals("200", askproductvideoCall.getString("code"), "验证code");
			pve = LiveRoomWapper.selectProductvideoExplain(pid);
			Logger.verifyEquals(1, pve.getExplainNum(), "验证ExplainNum");
			Logger.verifyEquals(sellerId, pve.getUserId(), "验证userid");
			// 0 待录制
			
			Logger.verifyEquals(0, pve.getExplainStatus(), "验证explainStatus");
			Logger.verifyEquals(createlive.getId(), pve.getLiveId(), "验证LiveId");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_AskProductVideo_014() {
		Logger.start(true, "求讲解成功、计数器+1");
		try {
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String pid = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(sellerId));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), createlive.getUserId());
			askproductvideoBean.setLiveId(createlive.getId());
			askproductvideoBean.setProductId(pid);
			// 普通用户id 暂时无用
			askproductvideoBean.setUserId(1);
			askproductvideoCall.setData(askproductvideoBean);

			Integer counter = CounterCallService.getSingleValueByLiveId(createlive.getId());
			askproductvideoCall.callService();	
			Integer counter1 = CounterCallService.getSingleValueByLiveId(createlive.getId());
			Logger.verifyEquals(counter+1, counter1, "添加商品之后，计数器+1");
			
			Logger.verifyEquals("200", askproductvideoCall.getString("code"), "验证code");
			ProductVideoExplain pve = LiveRoomWapper.selectProductvideoExplain(pid);
			Logger.verifyEquals(1, pve.getExplainNum(), "验证ExplainNum");
			Logger.verifyEquals(sellerId, pve.getUserId(), "验证userid");
			// 0 待录制
			Logger.verifyEquals(0, pve.getExplainStatus(), "验证explainStatus");
			Logger.verifyEquals(createlive.getId(), pve.getLiveId(), "验证LiveId");
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
}